package com.yonyou.pmclouds.basecom.bp.template;

import com.yonyou.pmclouds.basecom.bp.processor.GenIdProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.UpdateTsProcessor;
import com.yonyou.pmclouds.basecom.dao.BaseMapper;
import com.yonyou.pmclouds.basecom.entity.Entity;
import com.yonyou.pmclouds.basecom.exception.BusinessException;

/**
 * 单个实体的插入模板
 *
 * @author mapeif
 */
public class SingleInsertBpTemplate<T extends Entity> extends SingleOperateBpTemplate<T> {
    private BaseMapper<T> mapper;

    public SingleInsertBpTemplate(BaseMapper<T> mapper) {
        this(mapper, true);
    }

    public SingleInsertBpTemplate(BaseMapper<T> mapper, boolean genId) {
        this.mapper = mapper;
        if (genId) {
            addBeforeProcessor(new GenIdProcessor<T>());
        }
        addBeforeProcessor(new UpdateTsProcessor<T>());
    }

    @Override
    protected void operateDirect(T vo, T orig) {
        mapper.insert(vo);
    }

    public T insert(T vo) throws BusinessException {
        return execute(vo);
    }
}
